package cn.shadow.module.scene;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//


import com.google.common.annotations.VisibleForTesting;
import com.lmax.disruptor.AlertException;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.EventTranslatorThreeArg;
import com.lmax.disruptor.EventTranslatorTwoArg;
import com.lmax.disruptor.EventTranslatorVararg;
import com.lmax.disruptor.InsufficientCapacityException;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.Sequence;
import com.lmax.disruptor.SequenceBarrier;
import com.lmax.disruptor.TimeoutException;
import com.lmax.disruptor.WaitStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RingBufferWrapper<E extends RingBufferEvent> implements RingEventSource<E> {
    private static final Logger b = LoggerFactory.getLogger(RingBufferWrapper.class);
    @VisibleForTesting
    final RingBuffer<E> a;
    private final Sequence c;
    private long d;
    private static final RingBufferWrapper.ST e = new RingBufferWrapper.ST();

    public RingBufferWrapper(EventFactory<E> var1, int var2, boolean var3) {
        this.a = var3 ? RingBuffer.createSingleProducer(var1, var2, e) : RingBuffer.createMultiProducer(var1, var2, e);
        this.c = new Sequence();
        this.a.addGatingSequences(new Sequence[]{this.c});
    }

    public boolean hasAvailableCapacity(int var1) {
        return this.a.hasAvailableCapacity(var1);
    }

    public long tryNext() throws InsufficientCapacityException {
        return this.a.tryNext();
    }

    public void publish(long var1) {
        this.a.publish(var1);
    }

    public E get(long var1) {
        return this.a.get(var1);
    }

    public boolean tryPublishEvent(EventTranslator<E> var1) {
        return this.a.tryPublishEvent(var1);
    }

    public <A> boolean tryPublishEvent(EventTranslatorOneArg<E, A> var1, A var2) {
        return this.a.tryPublishEvent(var1, var2);
    }

    public <A, B> boolean tryPublishEvent(EventTranslatorTwoArg<E, A, B> var1, A var2, B var3) {
        return this.a.tryPublishEvent(var1, var2, var3);
    }

    public <A, B, C> boolean tryPublishEvent(EventTranslatorThreeArg<E, A, B, C> var1, A var2, B var3, C var4) {
        return this.a.tryPublishEvent(var1, var2, var3, var4);
    }

    public boolean tryPublishEvent(EventTranslatorVararg<E> var1, Object... var2) {
        return this.a.tryPublishEvent(var1, var2);
    }

    public boolean hasEventsToHandle() {
        return this.a.isPublished(this.d);
    }

    public void handleEvents() {
        long var1 = this.d;
        long var3 = this.a(var1);
        if (var3 >= var1) {
            try {
                for(long var6 = var1; var6 <= var3; ++var6) {
                    try {
                        RingBufferEvent var5 = (RingBufferEvent)this.a.get(var6);

                        try {
                            var5.handle();
                        } finally {
                            var5.cleanUp();
                        }
                    } catch (Throwable var17) {
                        b.error("RingBufferWrapper 处理事件出错", var17);
                    }
                }
            } finally {
                this.d = var3 + 1L;
                this.c.set(var3);
            }

        }
    }

    @VisibleForTesting
    long a(long var1) {
        long var3;
        for(var3 = var1; this.a.isPublished(var3); ++var3) {
        }

        return var3 - 1L;
    }

    public void handleEvents(RingEventHandler<E> var1) {
        long var2 = this.d;
        long var4 = this.a(var2);
        if (var4 >= var2) {
            try {
                for(long var7 = var2; var7 <= var4; ++var7) {
                    try {
                        RingBufferEvent var6 =this.a.get(var7);

                        try {
                            var1.handle((E) var6);
                        } finally {
                            var6.cleanUp();
                        }
                    } catch (Throwable var18) {
                        b.error("RingBufferWrapper 处理事件出错", var18);
                    }
                }
            } finally {
                this.d = var4 + 1L;
                this.c.set(var4);
            }

        }
    }

    private static class ST implements WaitStrategy {
        private ST() {
        }

        public long waitFor(long var1, Sequence var3, Sequence var4, SequenceBarrier var5) throws AlertException, InterruptedException, TimeoutException {
            throw new UnsupportedOperationException();
        }

        public void signalAllWhenBlocking() {
        }
    }
}
